package conf

import (
	"fmt"
	"gitee.com/youbeiwuhuan/gin-practice/go-gorm2/gormv2"
	"gitee.com/youbeiwuhuan/gin-practice/go-gorm2/zapservice"
	"github.com/spf13/viper"
	"io"
	"os"
)

type AppConfig struct {
	Zap   zapservice.ZapConfig `mapstructure:"zap"`
	Gorm2 gorm_v2.Gorm2Config  `mapstructure:"gormv2"`
}

func Load(file io.Reader) AppConfig {
	config := viper.New()
	//开始读取环境变量，如果没有调用这个函数，则下面无法读取到path的值
	config.AutomaticEnv()
	config.SetConfigType("yaml")
	config.ReadConfig(file)

	// 配置读到结构体
	var cfg AppConfig
	if err := config.Unmarshal(&cfg); err != nil {
		fmt.Println(err)
		os.Exit(-1)
	} else {
		fmt.Println(cfg)
	}

	return cfg
}
